package com.huarui.permission.inteceptor;

import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.struts2.StrutsStatics;
import org.ewaf.framework.cookie.CookieManager;

import com.huarui.permission.dataobject.Action;
import com.huarui.permission.service.ActionService;
import com.huarui.permission.service.PermissionService;
import com.huarui.user.model.CookieConstants;
import com.huarui.user.model.User;
import com.huarui.user.security.UserHolder;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class UserAuthenticationInterceptor extends AbstractInterceptor {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private PermissionService permissionService;

	private ActionService actionService;

	public ActionService getActionService() {
		return actionService;
	}

	public void setActionService(ActionService actionService) {
		this.actionService = actionService;
	}

	private Properties env;

	public Properties getEnv() {
		return env;
	}

	public void setEnv(Properties env) {
		this.env = env;
	}

	public PermissionService getPermissionService() {
		return permissionService;
	}

	public void setPermissionService(PermissionService permissionService) {
		this.permissionService = permissionService;
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		/*
		 * HttpServletRequest request = (HttpServletRequest) invocation
		 * .getInvocationContext().get(StrutsStatics.HTTP_REQUEST);
		 */

		final String action = invocation.getAction().getClass().getSimpleName();
		final String method = invocation.getProxy().getMethod();
		
		if (UserHolder.getCurrentSessionMessage() == null
				|| UserHolder.getCurrentSessionMessage().isLogined() == false) {

			Integer permissionId = actionService
					.getPermissionId(action, method);

			/**
			 * 目的action不在action表(黑名单)中，不需要限制
			 */
			if (permissionId == null||permissionId == 0) {
				return invocation.invoke();
			}

			return "login";
		}

		/**
		 * 登录用户
		 */
		Integer companyId = UserHolder.getCurrentSessionMessage()
				.getCompanyId();

		if (companyId == null || companyId==0) {
			/**
			 * 该用户未注册公司也不属于任何公司
			 */
			Object obj = CollectionUtils.find(permissionService
					.getActionsGranted2LoginedUser(), new Predicate() {

				@Override
				public boolean evaluate(Object object) {
					Action a = (Action) object;
					return a.getActionName().equalsIgnoreCase(action)
							&& a.getMethodName().equalsIgnoreCase(method);
				}
			});

			if (obj == null) {
				/**
				 * 无公司用户不允许访问该url , 跳转到注册公司页面
				 */
				return "error";
			}

			return invocation.invoke();

		}

		Integer permissionId = actionService.getPermissionId(action, method);

		if (permissionId == null||permissionId==0) {
			/**
			 * 不需要权限控制
			 */
			return invocation.invoke();
		}

		if (permissionService.hasPermission(permissionId) == 0) {

			return "error";

		}

		return invocation.invoke();
	}

}
